
const app = getApp()
const api = app.globalData.api

Page({

  /**
   * 页面的初始数据
   */
  data: {
    info:{},
    reason:'',
    recordDate:'',
    pay_type:'',
    time_popup_show:false,
    typeOption:[],
    type_popup_show:false,
    isBack:false,
    commit_name_id:'',
    commit_name:'',
    staffName:'',
    staffId:'',
    staffSelect:[],
    travelWay:[],
    goWay:[],
    returnWay:[],
    isTicket:'',
    isHotel:'',
    chooseType:'',//选择员工的类型
    startTime:'',
    endTime:'',
    childId:'',
    children:[
      {
        id: Math.random().toString(36).substring(2),
        money:'',
        feeType:''
      }
    ],
  },
    /**删除同行人 */
    delStaff(e){
      let id = e.currentTarget.dataset.id;
      let staffSelect = this.data.staffSelect.filter(item=>item.id!==id);
      this.setData({
        staffSelect:staffSelect
      })
    },
    
    /**删除元素 */
    delChild(e){
      let id = e.currentTarget.dataset.id;
      let arr = this.data.children
      let newArr = arr.filter(item => item.id !=id );
      this.setData({
        children:newArr
      })
    },
  /**添加报销明细 */
  addChild(){
    let id = Math.random().toString(36).substring(2);
    let child={
      id:id,
      feeType:'',
      money:'',
  
    }
    let arr = this.data.children
    arr.push(child);
    this.setData({
      children:arr
    })
  },
  onRadioChange(event){
    let type = event.currentTarget.dataset.type;
    if( type == 'ticket'){
      this.setData({
        isTicket: event.detail,
      });
    }else if(type == 'hotel'){
      this.setData({
        isHotel: event.detail,
      });
    }else if(type == 'advance'){
      this.setData({
        isAdvance: event.detail,
      });
    }
   
  },
  /**复选框编辑 */
  onCheckChange(e){
    let type = e.currentTarget.dataset.type;
    if(type == 'goWay'){
      this.setData({
        goWay: e.detail,
      });
    }else{
      this.setData({
        returnWay: e.detail,
      });
    }
    

  },
  
  /**重新组装明细数组 */
  reChildren(id,value,field){
    let children = this.data.children;
    let newChildren=[];
    children.forEach((v,i)=>{
      if(v.id==id){
        switch(field){
          case 'feeType':
            v.feeType=value;
            newChildren.push(v);
            break;
            case 'money':
              v.money=value;
              newChildren.push(v);
              break;
        }
      }else{
        newChildren.push(v);
      }
    })
    return newChildren;
  },
 /**明细内容改变时修改 */
 changeMx(e){
  let field = e.currentTarget.dataset.field;
  let id = e.currentTarget.dataset.id;
  this.setData({
    children:this.reChildren(id,e.detail,field)
  })
},
  /**
   * 选择费用类型
   */
  showTypePopup(e){
    let that = this; 
    let id = e.currentTarget.dataset.id;
    api.curlPost('?cla=bussinessTripApp&fun=search').then((res)=>{
      that.setData({
        childId:id,
        type_popup_show:true,
        typeOption:res.data.result.feeOption
      })
    })
  },

  /**费用类型确认 */
  onTypeConfirm(event){
    const { picker, value, index } = event.detail;
    let children = this.reChildren(this.data.childId,value,'feeType');
    this.setData({
      children,
      type_popup_show:false
    })
  },
  onTypeCancel(e){
    this.setData({
      type_popup_show:false
    })
  },
  
/**选择时间 */
chooseTime(e){
  let type = e.currentTarget.dataset.type;
  let currentTimestamp = '';
  if(type=='startTime'){
    currentTimestamp = this.data.startTime?new Date(this.data.startTime).getTime():new Date().getTime();
  }else{
    currentTimestamp = this.data.endTime?new Date(this.data.endTime).getTime():new Date().getTime();
  }
  this.setData({
    nowTimeType:type,
    time_popup_show:true,
    currentTimestamp:currentTimestamp
  })
},
/**日期选择确认 */
timeConfirm(e){
  if(this.data.nowTimeType=='startTime'){
    this.setData({
      startTime:api.formatDateCRM('Y-m-d H:i:s',e.detail),
      time_popup_show:false
    })
  }else{
    this.setData({
      endTime:api.formatDateCRM('Y-m-d H:i:s',e.detail),
      time_popup_show:false
    })
  }
 
},
timeCancel(e){
  this.setData({
    time_popup_show:false
  })
},
  commit(e){
    let para = e.detail.value;
    para.staffId=this.data.staffId;
    para.member = JSON.stringify(this.data.staffSelect);
    para.id=this.data.info.id?this.data.info.id:'',
    para.children = JSON.stringify(this.data.children);
    para.goWay = JSON.stringify(this.data.goWay);
    para.returnWay = JSON.stringify(this.data.returnWay);
    api.curlPost('?cla=bussinessTripApp&fun=commitEdit',para).then((res)=>{
      wx.redirectTo({
        url: '../bussinessTripDetail/bussinessTripDetail?id='+res.data.id
      })
    })
  },




  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = api.isBlank(options.id)?'':options.id;
    let that = this;
    api.curlPost('?cla=bussinessTripApp&fun=search',{}).then((res)=>{
        that.setData({
          travelWay:res.data.result.travelWay,
        })
      })
      if(id){
      api.curlPost('?cla=bussinessTripApp&fun=detail',{id:id}).then((res)=>{
        that.setData({
          info:res.data.result.info,
          startTime:res.data.result.info.startTime,
          endTime:res.data.result.info.endTime,
          staffSelect:res.data.result.info.member,
          staffId:res.data.result.info.staffId,
          staffName:res.data.result.info.staffName,
          returnWay:res.data.result.info.returnWay,
          goWay:res.data.result.info.goWay,
          isHotel:res.data.result.info.isHotel,
          isTicket:res.data.result.info.isTicket,
          isAdvance:res.data.result.info.isAdvance,
        })
        if(!api.isBlank(res.data.result.list)){
          that.setData({
            children:res.data.result.list
          })
        }
      })
      }
  },
 
  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  chooseStaff(e){
    let type = e.currentTarget.dataset.type;
    this.setData({
      chooseType:type
    })
    wx.navigateTo({
      url: '../staff/staff',
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    if(this.data.isBack){
      if(this.data.chooseType == 'staffId'){
        this.setData({
          isBack:false,
          staffName:this.data.commit_name,
          staffId:this.data.commit_name_id
        });
      }else if(this.data.chooseType == 'partner'){
        if(that.data.commit_name_id){
          let flag = false;
          let resultArr=[];
          this.data.staffSelect.forEach((item)=>{
            if(item.id == that.data.commit_name_id){
              flag = true;
            }
            resultArr.push(item);
          })
          if(!flag){
            resultArr.push({id:this.data.commit_name_id,name:this.data.commit_name})
          }
          this.setData({
            staffSelect:resultArr,
            isBack:false,
            commit_name:'',
            commit_name_id:''
          })
        }
      }
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})